<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Think\Controller;
/**
 * 后台用户控制器
 * @author jry <598821125@qq.com>
 */
class ServiceController extends HomeController{
	
	//客户服务记录列表
	public function index(){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		$ding_id=$user_info['userid'];
		$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('id');
		 //搜索
        $keyword = I('keyword', '', 'string');
        $condition = array('like','%'.$keyword.'%');
        if($condition){
			$map['id|service_content'] = array($condition,$condition,'_multi'=>true);
		}
		
		$keywords=I('keywords');
		if(!empty($keywords)){
			$map['service_body']=$keywords;
		}
		
        //获取所有用户
		$map['creater']=$uid;
       $map['status'] = array('egt', '0'); //禁用和正常状态
        $data_list = D('CustomerService')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
        $page = new \Common\Util\Page(D('CustomerService')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));

			foreach($data_list as $key=>&$val){
				$service_body=D('Descriptions')->where(array('fieldname'=>'service_body','fieldvalue'=>$val['service_body']))->find();
				if ($val['service_body']==1) {//项目
					$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('project_number');
				}elseif ($val['service_body']==2){//客户
					$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('customer_name');
				}elseif ($val['service_body']==5){//代理
					$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('domin');
				}elseif ($val['service_body']==6){//服务器
				$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('server_name');
				}else{
					$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('name');
				}
					
				$val['service_body']=$service_body['title']."：".$bodyname;
				$hh['tablename']='service_type';
				$hh['fieldvalue']=$val['service_type'];
				$service_type=D('Descriptions')->where($hh)->field('title')->find();
				$val['service_type']=$service_type['title'];
				$val['service_time']=date('Y-m-d',$val['service_time']);
// 			$maap['id']=$val['projectid'];//项目id
// 			$mapk['uid']=$val['customer_service_id'];//客服id
// 			$project=D('Project');
// 			$project_name=$project->where($maap)->field('project_number')->find();//项目名称
// 			$employee=D('Employee');
// 			$customer_service_name=$employee->where($mapk)->field('name')->find();//客服姓名
// 			$val['customer_service_id']=$customer_service_name['name'];
// 			$val['projectid']=$project_name['project_number'];
// 			$hh['tablename']='service_type';
// 			$hh['fieldvalue']=$val['service_type'];
// 			$service_type=D('Descriptions')->where($hh)->field('title')->find();
// 			$val['kh_id']=$val['customerid'];
//             $val['customerid']=get_customer_info($val['customerid'], 'customer_name');
// 			 $val['service_type']=$service_type['title'];
			
			// $val['service_content']=get_str($val['service_content'],0,20).'<button type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="<h4>服务内容</h4>'.$val['service_content'].'<h4>服务结果</h4>'.$val['service_result'].'">更多</button>;
			//$val['service_content']='<a data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="<h4>服务内容</h4>'.$val['service_content'].'<h4>服务结果</h4>'.$val['service_result'].'">'.get_str($val['service_content'],0,20).'</a>';
			}
			$service_body=D('Descriptions')->where("fieldname='service_body'")->select();
 
		$this->assign('service_body', $service_body);
        $this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
        $this->assign('page', $page->show());
        $this->assign('data_list', $data_list);
        $this->assign('user_info', $user_info);
        $this->assign('meta_title', '我的服务');
        $this->display();
	}
	public function all(){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		$ding_id=$user_info['userid'];
		//搜索
		$keyword = I('keyword', '', 'string');
		$condition = array('like','%'.$keyword.'%');
		$map['id|service_content'] = array($condition,$condition,'_multi'=>true);
		
		$keywords=I('keywords');
		if(!empty($keywords)){
			$map['service_body']=$keywords;
		}
			
		//获取所有用户
		$map['status'] = array('egt', '0'); //禁用和正常状态
		$data_list = D('CustomerService')->page(!empty($_GET["p"])?$_GET["p"]:1, C('ADMIN_PAGE_ROWS'))->where($map)->order('id desc')->select();
		$page = new \Common\Util\Page(D('CustomerService')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));
		
		foreach($data_list as $key=>&$val){
			$service_body=D('Descriptions')->where(array('fieldname'=>'service_body','fieldvalue'=>$val['service_body']))->find();
			
			if ($val['service_body']==1) {//项目
				$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('project_number');
			}elseif ($val['service_body']==2){//客户
				$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('customer_name');
			}elseif ($val['service_body']==5){//代理
				$webid=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('name');
				$bodyname=M('Website')->where(array('id'=>$webid))->getField('name');
			}elseif ($val['service_body']==6){//服务器
				$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('server_name');
			}else{
				$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('name');
			}
			
			$val['service_body']=$service_body['title']."：".$bodyname;
			$hh['tablename']='service_type';
			$hh['fieldvalue']=$val['service_type'];
			$service_type=D('Descriptions')->where($hh)->field('title')->find();
			$val['service_type']=$service_type['title'];
			$val['service_time']=date('Y-m-d',$val['service_time']);
// 			$maap['id']=$val['projectid'];//项目id
// 			$mapk['uid']=$val['customer_service_id'];//客服id
// 			$project=D('Project');
// 			$project_name=$project->where($maap)->field('project_number')->find();//项目名称
// 			$employee=D('Employee');
// 			$customer_service_name=$employee->where($mapk)->field('name')->find();//客服姓名
// 			$val['customer_service_id']=$customer_service_name['name'];
// 			$val['projectid']=$project_name['project_number'];
// 			$hh['tablename']='service_type';
// 			$hh['fieldvalue']=$val['service_type'];
// 			$service_type=D('Descriptions')->where($hh)->field('title')->find();
// 			$val['kh_id']=$val['customerid'];
// 			$val['customerid']=get_customer_info($val['customerid'], 'customer_name');
// 			$val['service_type']=$service_type['title'];
			
			// $val['service_content']=get_str($val['service_content'],0,20).'<button type="button" class="btn btn-default" data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="<h4>服务内容</h4>'.$val['service_content'].'<h4>服务结果</h4>'.$val['service_result'].'">更多</button>;
			//$val['service_content']='<a data-container="body" data-toggle="popover" data-placement="top" data-html="true" data-content="<h4>服务内容</h4>'.$val['service_content'].'<h4>服务结果</h4>'.$val['service_result'].'">'.get_str($val['service_content'],0,20).'</a>';
		}
		$service_body=D('Descriptions')->where("fieldname='service_body'")->select();
 
		$this->assign('service_body', $service_body);
		$this->assign('HTTP_HOST', C('DING_HTTP_HOST'));
		$this->assign('page', $page->show());
		$this->assign('data_list', $data_list);
		$this->assign('user_info', $user_info);
		$this->assign('meta_title', '我的服务');
		$this->display();
	}
//新增
	public function add(){
		$user_info=session('DING_USER');
		if (!$user_info) {
			header('Location: '.U('Home/Task/index'));
		}
		$ding_id=$user_info['userid'];
		$uid=M('Employee')->where(array('ding_id'=>$ding_id))->getField('id');
		if(IS_POST){

            $_POST['status']=1;
            $_POST['service_time']=time();
            $_POST['creater'] = $uid;
            $_POST['inputer'] = $user_info['name'];
			$id = M('CustomerService')->add($_POST);
                if($id){
                    $this->success('新增成功', U('index'));
                }else{
                    $this->error('新增失败');
                }

        }else{
        	$service_body=D('Descriptions')->where(array('fieldname'=>'service_body'))->select();
        	$service_type=D('Descriptions')->where("tablename='service_type'")->select();
            
        	$project_number=M('Project')->field('id,project_number as title')->select();
            $customer=M("Customer")->field("id,customer_name as title")->select();
            $Website=M('Website')->field('id,name as title')->select();
            $Domain=M("Domain")->field("id,name as title")->select();
            $Reverse=M('Reverse')->field('id,name')->select();
            foreach ($Reverse as $rk=>$rv){
            	$Reverse[$rk]['title']=M('Website')->where(array('id'=>$rv['name']))->getField('name');
            }
            $Database=M("Database")->field("id,name as title")->select();
            $Server=M('Server')->field('id,server_name as title')->select();
            $Master=M("Master")->field("id,name as title")->select();
            
            $this->assign('project_number', $project_number);
            $this->assign('customer', $customer);
            $this->assign('website', $Website);
            $this->assign('domain', $Domain);
            $this->assign('reverse', $Reverse);
            $this->assign('database', $Database);
            $this->assign('server', $Server);
            $this->assign('master', $Master);
            
            $this->assign('service_type', $service_type);
            $this->assign('service_body', $service_body);
            $this->assign('user_info', $user_info);
            $this->assign('meta_title', '新增服务');
            $this->display();
        }
		
}
/**
 * 编辑
 *
 * @author jry <598821125@qq.com>
 */
// public function edit($id)
// {
// 	$user_info=session('DING_USER');
// 	if (!$user_info) {
// 		header('Location: '.U('Home/Task/index'));
// 	}
// 	if (IS_POST) {
// 		$_POST['processor'] = M('Employee')->where(array('ding_id'=>$_POST["processor"]))->getField('uid');
				
//             $project_object = D('Project');

//             if ($project_object->save($_POST)) {
//             		$this->success('更新成功', U('index'));
// 			} else {
//                 $this->error('更新失败', $project_object->getError());
//             }
//     } else {

//             $info = D('CustomerService')->find($id);
//             $processor['id']=M('Employee')->where(array('uid'=>$info['processor']))->getField('ding_id');
//             $processor['name']=M('Employee')->where(array('uid'=>$info['processor']))->getField('name');
                		
//             $project_number=M('Project')->field('id,project_number as title')->select();
//             $kefu_arr=M("employee")->where("department=7")->field("uid as id,name as title")->select();
//             $cus=M("Customer")->field("id,customer_name")->select();
//             foreach($cus as $k=>$v){
//             	$cuss[$v['id']]=$v['customer_name'];
//             }
//             $service_type=D('Descriptions')->where("tablename='service_type'")->select();
            
//             $this->assign('customer', $cuss);
//             $this->assign('service_type', $service_type);
//             $this->assign('kefu_arr', $kefu_arr);
//             $this->assign('project_number', $project_number);
//             $this->assign('processor', $processor);
//             $this->assign('info', $info);
//             $this->assign('user_info', $user_info);
//             $this->assign('meta_title', '编辑服务');
//             $this->display();
//        }
//    }
 


/**
 * 服务详情页
 */
public function serviceinfo(){
	$id=I('id');
	$map['id']=$id;
	$val = M('CustomerService')->where($map)->find();

	$service_body=D('Descriptions')->where(array('fieldname'=>'service_body','fieldvalue'=>$val['service_body']))->find();
		
	if ($val['service_body']==1) {//项目
		$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('project_number');
	}elseif ($val['service_body']==2){//客户
		$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('customer_name');
	}elseif ($val['service_body']==5){//代理
		$webid=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('name');
		$bodyname=M('Website')->where(array('id'=>$webid))->getField('name');
	}elseif ($val['service_body']==6){//服务器
		$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('server_name');
	}else{
		$bodyname=M($service_body['tablename'])->where(array('id'=>$val['service_bodyid']))->getField('name');
	}
		
	$val['service_body']=$service_body['title']."：".$bodyname;
	$hh['tablename']='service_type';
	$hh['fieldvalue']=$val['service_type'];
	$service_type=D('Descriptions')->where($hh)->field('title')->find();
	$val['service_type']=$service_type['title'];
	$val['service_time']=date('Y-m-d',$val['service_time']);
	
// 	$map['id']=$val['projectid'];//项目id
// 	$mapk['uid']=$val['customer_service_id'];//客服id
// 	$project=D('Project');
// 	$project_name=$project->where($map)->field('project_name')->find();//项目名称
// 	$employee=D('Employee');
// 	$customer_service_name=$employee->where($mapk)->field('name')->find();//客服姓名
// 	$val['customer_service_id']=$customer_service_name['name'];
// 	$val['projectid']=$project_name['project_name'];
// 	$hh['tablename']='service_type';
// 	$hh['fieldvalue']=$val['service_type'];
// 	$service_type=D('Descriptions')->where($hh)->field('title')->find();
	
// 	$val['service_type']=$service_type['title'];
// 	$val['service_time']=date('Y-m-d',$val['service_time']);
		
	$this->assign('val', $val);
	$this->assign('meta_title', '项目详情');
	$this->display();
	 
}

/**
 * 获取服务
 */
public function getservice(){
	$service_bodyid=I('id');
	$tablename=I('type');
	$service_body=D('Descriptions')->where(array('fieldname'=>'service_body','tablename'=>$tablename))->getField('fieldvalue');
	$map=array('service_body'=>$service_body,'service_bodyid'=>$service_bodyid);
	$info=M('CustomerService')->where($map)->order('service_time desc')->select();
	$page = new \Common\Util\Page(D('CustomerService')->where($map)->count(), C('ADMIN_PAGE_ROWS' ));
	
	$this->assign('info', $info);
	$this->assign('page', $page->show());
	$this->assign('meta_title', '服务信息');
	$this->display();
}







}